/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gatech.egg.l0cUs.controller.author;

import edu.gatech.egg.l0cUs.service.QuestDao;
import edu.gatech.egg.l0cUs.domain.Quest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author summerspittman
 */
@Controller
@RequestMapping("/author/quest/**")
public class QuestController {

    @Autowired
    private QuestDao questService;

    @RequestMapping(value = "/author/quest/{id}", method = RequestMethod.GET)
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {
        Assert.notNull(id, "Identifier must be provided.");
        modelMap.addAttribute("quest", questService.find(id));
        return "/quest/show";
    }

    @RequestMapping(value = "/author/quest", method = RequestMethod.GET)
    public String list(ModelMap modelMap) {
        modelMap.addAttribute("quests", questService.findAll());
        return "/quest/list";
    }

    @RequestMapping(value = "/author/quest/{id}/form", method = RequestMethod.GET)
    public String form(@PathVariable("id") Long id,ModelMap modelMap) {
        Assert.notNull(id, "Identifier must be provided.");
        modelMap.addAttribute("quest", questService.find(id));
        return "/quest/create";
    }

    @RequestMapping(value = "/author/quest/form", method = RequestMethod.GET)
    public String form(ModelMap modelMap) {
        modelMap.addAttribute("quest", new Quest());
        return "/quest/create";
    }

    @RequestMapping(value = "/author/quest", method = RequestMethod.POST)
    public String create(@ModelAttribute("quest") Quest quest, BindingResult result) {
        Assert.notNull(quest, "Quest must be provided.");

        if (result.hasErrors()) {
            return "/quest/create";
        }
        questService.persist(quest);
        return "redirect:/author/quest.act";
    }
}
